package codelib.spring3struts2hibernate3.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import codelib.spring3struts2hibernate3.dao.BaseDAO;
import codelib.spring3struts2hibernate3.dao.UserDAO;
import codelib.spring3struts2hibernate3.dao.model.User;

@Component("UserDAOImpl")
@Transactional
public class UserDAOImpl extends BaseDAO implements UserDAO {

	@Override
	public void create(User user) {
		hibernateTemplate.save(user);
//		throw new RuntimeException("primary key duplicated.");
	}

	@Override
	public List<User> retrieveAll() {
		@SuppressWarnings("unchecked")
		List<User> listUser = (List<User>)hibernateTemplate
				.find("from User");
		return listUser;
	}

	@Override
	public List<User> retrieveByName(String userName) {
		@SuppressWarnings("unchecked")
		List<User> listStudent = (List<User>) hibernateTemplate
				.find("from User t where t.userName = ?", userName);
		return listStudent;
	}

	@Override
	public void update(User user) {
		hibernateTemplate.update(user);
	}

	@Override
	public void delete(int userId) {
		User user = hibernateTemplate.get(User.class,
				new Integer(userId));
		hibernateTemplate.delete(user);
	}
}
